Building a Crypto Backtester with the Coinbase API — Part 1: Setup
In the fast-paced world of cryptocurrency trading, automated trading bots have become a popular tool for both novice and seasoned traders. In this guide, I will show you how to make a basic live backtester using the coinbase API. You can use this platform to code your own algorithms and test them out. You can look at the entire project here
Our project is going to be divided into 5 steps:
- Setup
- Data collection
- Data processing
- Trading Strategies
- Order management
In this article, we’re gonna look at the initial project setup.
Requirements
This guide requires a intermediate to advanced level of proficiency in Python, so make sure to grind those yt tutorials before continuing on.
Libraries we’ll use:
- Pandas
- Numpy
- Matplotlib
- coinbase-advanced-py
- dotenv
- drawnow (For live matplotlib charts)
Don’t worry, we’ll go through each step in detail in this and the next articles.
Project Directory
Create a single folder in your workspace. That’s all we’ll need for now. You can give it a name you prefer, I’ll call mine “trading”
Setting up the Virtual-env
Virtual environments are a great way of storing the dependencies for a project. If you use conda, you can create a new conda environment with:
conda create -n trading_env python
If not, navigate to your project directory and create a venv in python as follows:
python3 -m venv trading_env
You should see a folder named trading_env in your project directory.
Downloading the libraries
Before downloading the libraries, make sure to load in your virtual environment. Again, if you’re using conda, you can enter:
conda activate trading_env
If you’re using Python’s venv , then in the project directory, enter the following command:
source trading_env/bin/activate
Then we can download all the libraries needed in a single command:
pip install pandas numpy coinbase-advanced-py matplotlib drawnow dotenv
Coinbase’s API
Make a wallet with Coinbase and log into your account using your wallet’s credentials. Then go over to their developer platform:
- Click Create API Key
- Give it a name and click on API restrictions
- Enable trade at the bottom
- Click Create and download
It will ask for an OTP. You now have your API keys to use.
Storing the API keys
If you’re never going to share your project with anyone, then storing API keys as variables in Python is fine. But, if you plan on uploading your code to github, or sharing it with anyone, it is never a good idea to expose your API keys. I will show you how to store API keys as an environment variable.
Open up the directory in your code editor, create a new file named .env . In the file paste in your API key and secret.
#in .env
COINBASE_API_NAME=
COINBASE_API_SECRET=
Don’t put quotations around your key and secret, spaces, or any other characters. When you share the code, make sure you don’t share this file.
Folder structure
Now that we have the API set up, we can begin coding the project. We will divide the folders into the different aspect of our backtester. Create these 7 folders:
- clients
- data_collection
- data_processing
- order_management
- trading_strategies
- utility
- visualization
Also create the main file:
main.py
Next steps
Finally, we have the project setup. We’re ready to code the backtester now!